package com.ymatou.iapi.cardservice.testcase;

import java.util.Arrays;

import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.cardservice.parameter.AccountingItemModel;
import com.ymatou.iapi.cardservice.parameter.CardInfo;
import com.ymatou.iapi.cardservice.parameter.StatusCodeEnum;
import com.ymatou.iapi.cardservice.service.AccountingCall;
import com.ymatou.iapi.cardservice.service.CheckAccountEntryCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.CardCallService;
import com.ymttest.database.annotation.SqlMapperFactory;
import com.ymttest.database.annotation.cardservice.CardServiceMapper;
import com.ymttest.utils.MapUtil;

public class Ts_CheckAccountEntry {

	private static CheckAccountEntryCall checkAccountEntryCall = new CheckAccountEntryCall();
	private static AccountingCall accountingCall = new AccountingCall();
	
	private static CardServiceMapper cardServiceMapper = SqlMapperFactory.getCardServiceMapper();
	
	private static String cardId = CardInfo.CardId1;
	private static String cardId_pswd = CardInfo.CardId1_Pswd;
	private static int userId = CardInfo.userId;
	private static String userName = CardInfo.userName;

	//卡初始金额
	private double denomination = 100.22;
		
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("查询一条出入账明细是否存在");
	}

	@Before
	public void caseUp() {
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(checkAccountEntryCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_CheckAccountEntry_001() throws Exception {
		Logger.start(true, "查询一条出入账明细是否存在 扣款明细");
		try {
			CardCallService.bindingCard(cardId, cardId_pswd, userId, userName);
			cardServiceMapper.updateCardAmountByCardId(cardId, denomination);
			
			
			AccountingItemModel atm = CardCallService.getPaymentAccountingItemModel(cardId,userId);
			atm.setAmount(denomination);
			
			JSONObject ret = accountingCall.call(Arrays.asList(atm));
			
			Logger.verifyEquals(StatusCodeEnum.Success.getCode(), ret.getInt("statusCode"), "check statusCode");
			Logger.verifyEquals(true, ret.getBoolean("success"), "check success");
			
			JSONObject ret1 = checkAccountEntryCall.call(cardId, atm.getBizCode(), atm.getBizNo());
			Logger.verifyEquals(StatusCodeEnum.Success.getCode(), ret.getInt("statusCode"), "check statusCode");
			Logger.verifyEquals(true, ret1.getBoolean("success"), "check success");
			Logger.verifyEquals(true, ret1.getJSONObject("data").getBoolean("exists"), "check exists");
			
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_CheckAccountEntry_002() throws Exception {
		Logger.start(true, "查询一条出入账明细是否存在 退款明细");
		try {
			//初始化卡，和金额
			CardCallService.bindingCard(cardId, cardId_pswd, userId, userName);
			cardServiceMapper.updateCardAmountByCardId(cardId, denomination);
			
			AccountingItemModel atm = CardCallService.getPaymentAccountingItemModel(cardId,userId);
			atm.setAmount(denomination);
			
			JSONObject ret = accountingCall.call(Arrays.asList(atm));
			
			Logger.verifyEquals(StatusCodeEnum.Success.getCode(), ret.getInt("statusCode"), "check statusCode");
			Logger.verifyEquals(true, ret.getBoolean("success"), "check success");
			
			
			//还款
			AccountingItemModel atm2 = CardCallService.getRefundAccountingItemModel(atm);
			ret = accountingCall.call(Arrays.asList(atm2));
			
			Logger.verifyEquals(StatusCodeEnum.Success.getCode(), ret.getInt("statusCode"), "check statusCode");
			Logger.verifyEquals(true, ret.getBoolean("success"), "check success");
			
		
			
			JSONObject ret1 = checkAccountEntryCall.call(cardId, atm2.getBizCode(), atm2.getBizNo());
			Logger.verifyEquals(StatusCodeEnum.Success.getCode(), ret.getInt("statusCode"), "check statusCode");
			Logger.verifyEquals(true, ret1.getBoolean("success"), "check success");
			Logger.verifyEquals(true, ret1.getJSONObject("data").getBoolean("exists"), "check exists");
			
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_CheckAccountEntry_003() throws Exception {
		Logger.start(true, "查询一条出入账明细是否存在 明细不存在");
		try {
			
			CardCallService.bindingCard(cardId, cardId_pswd, userId, userName);
			cardServiceMapper.updateCardAmountByCardId(cardId, denomination);
			
			
			AccountingItemModel atm = CardCallService.getPaymentAccountingItemModel(cardId,userId);
			atm.setAmount(denomination);
			
			JSONObject ret = accountingCall.call(Arrays.asList(atm));
			
			Logger.verifyEquals(StatusCodeEnum.Success.getCode(), ret.getInt("statusCode"), "check statusCode");
			Logger.verifyEquals(true, ret.getBoolean("success"), "check success");
			
			//card 不对
			JSONObject ret1 = checkAccountEntryCall.call(cardId+"1", atm.getBizCode(), atm.getBizNo());
			Logger.verifyEquals(StatusCodeEnum.Success.getCode(), ret.getInt("statusCode"), "check statusCode");
			Logger.verifyEquals(true, ret1.getBoolean("success"), "check success");
			Logger.verifyEquals(false, ret1.getJSONObject("data").getBoolean("exists"), "check exists");
			
			//bizCode 不对
			ret1 = checkAccountEntryCall.call(cardId, atm.getBizCode()+1, atm.getBizNo());
			Logger.verifyEquals(StatusCodeEnum.Success.getCode(), ret.getInt("statusCode"), "check statusCode");
			Logger.verifyEquals(true, ret1.getBoolean("success"), "check success");
			Logger.verifyEquals(false, ret1.getJSONObject("data").getBoolean("exists"), "check exists");
			
			//bizNo 不对
			ret1 = checkAccountEntryCall.call(cardId, atm.getBizCode(), atm.getBizNo()+"1");
			Logger.verifyEquals(StatusCodeEnum.Success.getCode(), ret.getInt("statusCode"), "check statusCode");
			Logger.verifyEquals(true, ret1.getBoolean("success"), "check success");
			Logger.verifyEquals(false, ret1.getJSONObject("data").getBoolean("exists"), "check exists");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_CheckAccountEntry_004() throws Exception {
		Logger.start(true, "查询一条出入账明细是否存在 数据缺失");
		try {
			CardCallService.bindingCard(cardId, cardId_pswd, userId, userName);
			cardServiceMapper.updateCardAmountByCardId(cardId, denomination);
			
			
			AccountingItemModel atm = CardCallService.getPaymentAccountingItemModel(cardId,userId);
			atm.setAmount(denomination);
			
			JSONObject ret = accountingCall.call(Arrays.asList(atm));
			
			Logger.verifyEquals(StatusCodeEnum.Success.getCode(), ret.getInt("statusCode"), "check statusCode");
			Logger.verifyEquals(true, ret.getBoolean("success"), "check success");
			
			
			//card 缺失
			JSONObject ret1 = checkAccountEntryCall.call("", atm.getBizCode(), atm.getBizNo());
			Logger.verifyEquals(StatusCodeEnum.ArgumentException.getCode(), ret1.getInt("statusCode"), "check statusCode");
			Logger.verifyEquals(false, ret1.getBoolean("success"), "check success");
			
			//bizCode 缺失
			ret1 = checkAccountEntryCall.call(cardId, 0, atm.getBizNo());
			Logger.verifyEquals(StatusCodeEnum.Success.getCode(), ret1.getInt("statusCode"), "check statusCode");
			Logger.verifyEquals(true, ret1.getBoolean("success"), "check success");
			Logger.verifyEquals(false, ret1.getJSONObject("data").getBoolean("exists"), "check exists");
			
			//bizNo 缺失
			ret1 = checkAccountEntryCall.call(cardId, atm.getBizCode(), "");
			Logger.verifyEquals(StatusCodeEnum.Success.getCode(), ret1.getInt("statusCode"), "check statusCode");
			Logger.verifyEquals(true, ret1.getBoolean("success"), "check success");
			Logger.verifyEquals(false, ret1.getJSONObject("data").getBoolean("exists"), "check exists");
			
			
			//appid 缺失
			ret1 = checkAccountEntryCall.callAndGetReturnData(MapUtil.hashMap("appId",""));
			Logger.verifyEquals(StatusCodeEnum.ArgumentException.getCode(), ret1.getInt("statusCode"), "check statusCode");
			Logger.verifyEquals(false, ret1.getBoolean("success"), "check success");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
}